// big button
// usage: <a href='url' onclick='callback' icon='icon-path or icon-class'>TEXT</a>
$(function() {
	$('.btn_big').each(function(index, item) {
		try {
			var itemObj = $(item);
			var span = itemObj.find('span');
			var strong = itemObj.find('strong');

			if(span.length == 0 && strong.length == 0) {
				itemObj.wrapInner('<span></span>');
			}
			
			var icon = itemObj.attr('icon');
			if(icon) {
				var ext = icon.split('.');
				ext = ext[ext.length-1];

				var firstChild = $(item.firstChild);
				if(ext == 'png' || ext == 'gif') {
					if($.browser.msie) {
						firstChild.css({'height':'26px', 'padding': '1px 9px 0 12px'})
							.prepend("<img src='"+icon+"' align='absmiddle' style='margin-right: 2px;'/>");
					} else {
						firstChild.css({'height':'26px'})
							.prepend("<img src='"+icon+"' align='absmiddle' style='margin-right: 2px;'/>");
					}
				} else {
					firstChild.wrapInner("<div class='"+icon+"'></div>");
				}
			}
		} catch(e) {
		}
	});
	
	$('.ext_select').each(function(index, item) {
		var $this = $(this);
		var val = $this.attr('es_value');
		
		if(val) {
			var text = $this.attr('es_text');
			if(!text) {
				text = "me";
			}
			
			var btn = $("<a href='#' class='btn_sml'><span>"+text+"</span></a>");
			btn.click(function() {
				$this.val(val);
				
				return false;
			});
			
			$this.after(btn);
		}
	});
	
	$('.tbl_type2 > table').each(function(index, item) {
		var $this = $(this);
		var tbody = $this.find('tbody');
		var trs = null;
		if(tbody.length > 0) {
			trs = tbody.find('tr');
		} else {
			trs = $this.find('tr').slice(1); // th
		}
		
		trs.mouseover(function() {
			$(this).addClass('on');
		}).mouseout(function() {
			$(this).removeClass('on');
		});
	});
	
	// Default Select
	$('.initSelect').each(function() {
		var $this = $(this);
		var val = $this.attr("initValue");
		if(val) {
			$this.val(val);				
		}
	});

	// panel
	$('.cui_panel').each(function(index, item) {
		var $this = $(this);
		if($this.find('.cui_panel_inner').length == 0) {
			$this.removeClass('cui_panel');
			var title = $this.attr('title');
			if(typeof title !== "string") {
				title = '';
			}
			
			var menu = $this.attr('menu');
			if(typeof menu !== "string") {
				memu = true;
			} else {
				menu = menu == 'true' ? true : false;
			}
			
			var cookieName = $this.attr('cookieName');
			if(typeof cookieName === "undefined") {
				cookieName = '';
			}
			
			$this.cuiPanel({'title': title, 'menu': menu, 'cookieName': cookieName});
		}
	});

	// bar
	$('.cui_bar').each(function(index, item) {
		var $this = $(this);
		if($this.find('.cui_bar_container').length == 0) {
			var cookieName = $this.attr('cookieName');
			if(typeof cookieName === "undefined") {
				cookieName = '';
			}
			
			$this.removeClass('cui_bar');
			$this.cuiBar({'cookieName': cookieName});
		}
	});
});

$.fn.extSelect = function(options) {
	var options = $.extend({
		'text':'me',
		'value': '',
		'click': function() {
		}
	}, options || {});
	
	if(options.value == '') {
		return;
	}
	
	var $this = $(this);
	var btn = $("<a href='#' class='btn_sml'><span>"+options.text+"</span></a>");
	btn.click(function() {
		$this.val(options.value);
		
		try {
			options.click();
		} catch(e) {
		}
		
		return false;
	});
	
	$this.after(btn);
};

$.fn.cuiPanel = function(option) {
	if(typeof option === "undefined") {
		option = {};
	}

	if(typeof option === "string") {
		option = {'target': option};
	}
	
	option = $.extend({
		'target': this,
		'title': '',
		'menu': true,
		'cookieName': ''
	}, option);

	var target = option.target;
	if(typeof target === "string") {
		target = $(target);
	}

	if(target) {
		var panel = target.find('.cui_panel');
		var hideBtn = panel.find('.cui_panel_hide');
		var showBtn = panel.find('.cui_panel_open');
		var wrapDiv = panel.find('.cui_panel_inner');
		
		if(hideBtn.length == 0) {
			hideBtn = $("<button title='Hide' class='cui_panel_hide' style='cursor:hand;'><span>Hide</span></button>"); 
		}

		if(showBtn.length == 0) {
			showBtn = $("<button title='Show' class='cui_panel_show' style='cursor:hand;'><span>Show</span></button>");
		}
		
		if(panel.length == 0) {
			var margin = {'margin-top':  target.css('margin-top'), 'margin-right':  target.css('margin-right'), 
					'margin-bottom':  target.css('margin-bottom'), 'margin-left':  target.css('margin-left')};
			
			panel = $("<div class='cui_panel'></div>");
			wrapDiv = $("<div class='cui_panel_inner'></div>");
			target.css({'margin': '0'})
			target.wrap(wrapDiv);

			wrapDiv = target.parent();
			wrapDiv.wrap(panel);
			
			wrapDiv.parent().css(margin);
			
			var title = $("<div class='cui_panel_title'><span>"+option.title+"</span></div>");
			
			if(option.menu) {
				var btnArea = $("<div class='cui_panel_title_buttons'></div>");
				btnArea.append(hideBtn);
				btnArea.append(showBtn);

				title.append(btnArea);
			}
			
			wrapDiv.before(title);

			hideBtn.show();
			showBtn.hide();
			
			if($.cookie) {
				if(option.cookieName != null && option.cookieName != '') {
					var status = $.cookie("cuiPanel_"+option.cookieName);
					if(status) {
						if(status == 'hide') {
							wrapDiv.hide();
							hideBtn.hide();
							showBtn.show();
						} 
					}
				}
			}
		}

		hideBtn.click(function() {
			if($.cookie) {
				if(option.cookieName != null && option.cookieName != '') {
					$.cookie("cuiPanel_"+option.cookieName, 'hide');
				}
			}
			
			wrapDiv.slideUp('fast');
			hideBtn.hide();
			showBtn.show();
		});

		showBtn.click(function() {
			if($.cookie) {
				if(option.cookieName != null && option.cookieName != '') {
					$.cookie("cuiPanel_"+option.cookieName, 'show');
				}
			}
			
			wrapDiv.slideDown('fast');
			hideBtn.show();
			showBtn.hide();
		});
	}
};

$.fn.cuiBar = function(option) {
	if(typeof option === "undefined") {
		option = {};
	}

	if(typeof option === "string") {
		option = {'target': option};
	}
	
	option = $.extend({
		'target': this,
		'cookieName': ''
	}, option);

	var target = option.target;
	if(typeof target === "string") {
		target = $(target);
	}

	if(target) {
		var panel = target.find('.cui_bar_container');
		var hideBtn = panel.find('.cui_bar_hide');
		var showBtn = panel.find('.cui_bar_show');
		var wrapDiv = panel.find('.cui_bar_inner');
		
		if(hideBtn.length == 0) {
			hideBtn = $("<button title='Hide' class='cui_bar_hide' style='cursor:hand;'><span>Hide</span></button>"); 
		}

		if(showBtn.length == 0) {
			showBtn = $("<button title='Show' class='cui_bar_show' style='cursor:hand;'><span>Show</span></button>");
		}
		
		if(panel.length == 0) {
			var margin = {'margin-top':  target.css('margin-top'), 'margin-right':  target.css('margin-right'), 
					'margin-bottom':  target.css('margin-bottom'), 'margin-left':  target.css('margin-left')};
			
			target.css({'border-bottom': '0px none', 'margin': '0'});
			panel = $("<div class='cui_bar_container'></div>");
			target.wrap(panel);
			
			target.parent().css(margin);
			
			bar = $("<div class='cui_bar'></div>");
			bar.append(hideBtn);
			bar.append(showBtn);
			
			target.after(bar);
			showBtn.hide();
			
			if($.cookie) {
				if(option.cookieName != null && option.cookieName != '') {
					var status = $.cookie("cuiBar_"+option.cookieName);
					if(status) {
						if(status == 'hide') {
							target.hide();
							hideBtn.hide();
							showBtn.show();
						} 
					}
				}
			}
		}

		hideBtn.click(function() {
			if($.cookie) {
				if(option.cookieName != null && option.cookieName != '') {
					$.cookie("cuiBar_"+option.cookieName, 'hide');
				}
			}
			
			target.slideUp('fast');
			hideBtn.hide();
			showBtn.show();
		});

		showBtn.click(function() {
			if($.cookie) {
				if(option.cookieName != null && option.cookieName != '') {
					$.cookie("cuiBar_"+option.cookieName, 'show');
				}
			}
			
			target.slideDown('fast');
			hideBtn.show();
			showBtn.hide();
		});
	}
}